package com.windoent.yugioh.ui.activity.registlogin;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.AppManager;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.bean.VerifyQQBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.LogUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.PhoneCode;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.lang.ref.WeakReference;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/4/28 14:15
 * desc   :绑定手机号验证码
 */
public class BindingCellPhoneCodeActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout mLlJump;
    private TextView mTvCellPhone;
    private TextView mTvCellPhoneModify;
    private PhoneCode mPhoneCode;
    private LinearLayout mLlReload;
    private TextView mBtnGetCode;
    private String bind_cell;
    private int third_type;
    private String auth_code;
    private String access_token;
    private String openid;
    private View mView;


    @Override
    public int getLayout() {
        return R.layout.activity_binding_cell_phone_code;
    }

    @Override
    public void initView() {
        super.initView();

        AppManager.getAppManager().addActivity(this);
        mView = (View) findViewById(R.id.view);

        immersiveStatusBar(mView);

        mLlJump = (LinearLayout) findViewById(R.id.ll_jump);
        mTvCellPhone = (TextView) findViewById(R.id.tv_cell_phone);
        mTvCellPhoneModify = (TextView) findViewById(R.id.tv_cell_phone_modify);
        mPhoneCode = (PhoneCode) findViewById(R.id.phone_code);
        mLlReload = (LinearLayout) findViewById(R.id.ll_reload);
        mBtnGetCode = (TextView) findViewById(R.id.btn_get_code);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        bind_cell = intent.getStringExtra("bind_cell");//手机号
        auth_code = intent.getStringExtra("auth_code");//微信授权码
        access_token = intent.getStringExtra("access_token");//QQ
        openid = intent.getStringExtra("openid");//QQ
        third_type = intent.getIntExtra(Constants.THIRD_TYPE, -1);//0微信1QQ
        mTvCellPhone.setText("+86\u0020" + bind_cell);
        startTimer();
    }


    @Override
    public void initListener() {
        super.initListener();
        mLlJump.setOnClickListener(this);
        mTvCellPhoneModify.setOnClickListener(this);
        mLlReload.setOnClickListener(this);
        mPhoneCode.setOnInputListener(new PhoneCode.OnInputListener() {
            @Override
            public void onSucess(String code) {
                //成功，跳转
                verifyCellPhoneCode(bind_cell, code);
            }

            @Override
            public void onInput(String phoneCode) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.ll_jump:
                startActivity(new Intent(mContext, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.tv_cell_phone_modify:
                finish();
                break;
            case R.id.ll_reload:
                getCellPhoneCode(bind_cell);
                startTimer();
                break;
            default:
                break;
        }
    }

    //倒计时
    private void startTimer() {
        mLlReload.setEnabled(false);
        SimpleUtils.startTimer(new WeakReference<TextView>(mBtnGetCode), "60s", 60, 1, new SimpleUtils.TimerListener() {
            @Override
            public void onFinished() {
                mLlReload.setEnabled(true);
            }
        });
    }

    //获取手机号验证码
    private void getCellPhoneCode(String cell) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(cell, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                LogUtils.d("response-->" + response);
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            startTimer();
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //验证手机短信
    private void verifyCellPhoneCode(final String bind_cell, final String code) {
        showLoadingDialog("");
        PhoneLive.verifyCellPhoneCode(bind_cell, code, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                LogUtils.d("responseveri-->" + response);
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                            if (third_type == 0) {
                                verifyWexin(auth_code, bind_cell);
                            } else if (third_type == 1) {
                                verifyQQTwo(access_token, openid, bind_cell, "0");
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //微信二次验证
    private void verifyWexin(String code, String phone) {
        if (BaseMethod.notNull(code)) {
            showLoadingDialog("");
            PhoneLive.verifyWeixin(code, "2", phone, "2", "0", new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    hideLoadingDialog();
                    ToastUtils.showText(e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    hideLoadingDialog();
                    LogUtils.d("lll-->" + response);
                    if (BaseUtils.isJSONValid(response)) {
                        try {
                            if (BaseUtils.codeIs0(mContext, response)) {
                                VerifyQQBean bean = gson.fromJson(response, VerifyQQBean.class);
                                VerifyQQBean.DataBean data = bean.getData();
                                if (data != null) {
                                    String token = data.getToken();
                                    SharedPreUtil.put(mContext, Constants.APPTOKEN, token);
                                    Intent intent = new Intent(mContext, CompletePersonInfoActivity.class);
                                    intent.putExtra("token", token);
                                    startActivity(intent);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtils.showText(getString(R.string.string_response_error));
                    }
                }
            });
        } else {
            ToastUtils.showText(getString(R.string.string_no_code));
        }
    }

    //QQ二次验证
    private void verifyQQTwo(String access_token, String openid, String phone, String user_id) {
        if (BaseMethod.notNull(access_token)) {
            showLoadingDialog("");
            PhoneLive.verifyQQTwo(access_token, openid, phone, "2", user_id, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    hideLoadingDialog();
                    ToastUtils.showText(e.getMessage());
                }

                @Override

                public void onResponse(String response, int id) {
                    hideLoadingDialog();
                    LogUtils.d("lll-->" + response);
                    if (BaseUtils.isJSONValid(response)) {
                        try {
                            if (BaseUtils.codeIs0(mContext, response)) {
                                VerifyQQBean bean = gson.fromJson(response, VerifyQQBean.class);
                                VerifyQQBean.DataBean data = bean.getData();
                                if (data != null) {
                                    String token = data.getToken();
                                    SharedPreUtil.put(mContext, Constants.APPTOKEN, token);
                                    Intent intent = new Intent(mContext, CompletePersonInfoActivity.class);
                                    intent.putExtra("token", token);
                                    startActivity(intent);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtils.showText(getString(R.string.string_response_error));
                    }
                }
            });
        } else {
            ToastUtils.showText(getString(R.string.string_no_code));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "登录注册绑定手机号验证码界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "登录注册绑定手机号验证码界面");
    }
}
